package com.apestech.api.condition.control;

import com.apestech.framework.json.JSONObject;
import com.apestech.framework.xml.condition.modul.Condition;
import com.apestech.framework.xml.condition.modul.Field;

import java.util.ArrayList;

public class ConditionRange extends ConditionBase {

    private boolean islower;
    private boolean isupper;
    private String sort;
    private String ladder;
    private int range;
    private ArrayList<String> resultlist;
    private int rcount;

    public ConditionRange(Condition condition, JSONObject input) {
        super(condition, input);
        islower=condition.getControl().isLower();
        isupper=condition.getControl().isUpper();
        sort=condition.getControl().getSort();
        ladder=condition.getControl().getLadderField();
        range=condition.getControl().getRange();
        resultlist=condition.getControl().getResultField();
        rcount=range-1;
        if(islower) rcount++;
        if(isupper) rcount++;
    }

    @Override
    public void saveResultMap(ArrayList<Field> fields, int fieldsize, JSONObject input, JSONObject output) throws Exception {
        for(int i=0; i<fieldsize; i++){
            String id=fields.get(i).getField();
            if(id.equals(ladder)){
                for(int c=1; c<=range; c++){
                    output.put(id+"_"+c, input.get(id+"_"+c));
                }
                continue;
            } else if(resultlist.contains(id)){
                for(int c=1; c<=rcount; c++){
                    output.put(id+"_"+c, input.get(id+"_"+c));
                }
                continue;
            }
            output.put(id, input.get(id));
        }

    }

    @Override
    public JSONObject finiExec() throws Exception {
        JSONObject input=super.getInput();
        JSONObject param=super.getParam();
        if(input.get("LADDER_NUM")!=null){
            Double num=input.getDoubleValue("LADDER_NUM");
            int inx=0;
            for(int r=1; r<=range; r++){
                Double value=param.getDoubleValue(ladder+"_"+r);
                if("STL".equals(sort)){  //STL小到大
                    if(islower && r==1){
                        if(num<value){  //100 200 300 400  233
                            inx=0;
                            break;
                        }
                    }
                    if(num<value || value==0){
                        inx=r;
                        break;
                    }
                } else if("LTS".equals(sort)){  //LTS大到小
                    if(islower && r==1){
                        if(num>value){
                            inx=0;
                            break;
                        }
                    }
                    if(num<value || value==0){
                        inx=r;
                        break;
                    }
                }
                if(r==range){
                    if(isupper){
                        inx=range+1;
                        break;
                    } else {
                        inx = -1;
                    }
                }
            }

            int rlen=resultlist.size();
            for(int c=1; c<=rcount; c++){
                super.getParam().remove(ladder+"_"+c);
            }
            for(int r=0; r<rlen; r++){
                String field=resultlist.get(r);
                if(inx==-1){
                    param.put(field, null);
                } else if(inx==0){
                    param.put(field, param.get(field+"_1"));
                } else {
                    if(islower){
                        param.put(field, param.get(field+"_"+inx));
                    } else {
                        param.put(field, param.get(field+"_"+(inx-1)));
                    }
                }
                for(int c=1; c<=rcount; c++){
                    param.remove(field+"_"+c);
                }
            }
        }
        return param;
    }

}
